package com.igeekhome.util;

import com.igeekhome.pojo.DOrderNumberList;
import com.igeekhome.pojo.WWaybillRule;

/**
 * @author 陈维豪
 */
public class WaybillUtils {

    //以下是有关单号规则表的有关方法

    /**
     *获取一个单号规则允许的最大的单号是多少
     * @param wWaybillRule
     * @return
     */
    public static Integer getMaxAllowNumber(WWaybillRule wWaybillRule){
        String beginChar = wWaybillRule.getBeginChar();
        String length = wWaybillRule.getLength();
        String endChar = "";
        for (int i=0;i<Integer.parseInt(length);i++){
            endChar += "9";
        }
        String maxAllowNumber = beginChar + endChar;

        return Integer.parseInt(maxAllowNumber);
    }

    /**
     * 判断分配的单号是否超过了单号规则规定的最大大小
     * @param wWaybillRule
     * @param endOrderNumber
     * @return
     */
    public static boolean checkIfAllowNumber(WWaybillRule wWaybillRule,Long endOrderNumber){
        Integer maxAllowNumber = getMaxAllowNumber(wWaybillRule);
        Integer endNumber = Integer.parseInt(endOrderNumber.toString());
        if(maxAllowNumber<=endNumber){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 将单号规则传入的开始字符进行转换，去除开头的0
     * @param wr
     * @return
     */
    public static String getBeginChar(WWaybillRule wr){
        String beginChar = wr.getBeginChar();
        Integer beginInteger = Integer.valueOf(beginChar);
        beginChar = beginInteger.toString();
        return beginChar;
    }

    /**
     * 获取正在使用的单号规则分配的最大单号
     * @param wr
     * @return
     */
    public static Integer getFirstMaxWayBillId(WWaybillRule wr){
        String beginChar = wr.getBeginChar();
        Integer beginInteger = Integer.valueOf(beginChar);
        beginChar = beginInteger.toString();
        wr.setBeginChar(beginChar);
        String length = wr.getLength();
        int i = (int)Math.pow(10, Integer.parseInt(length));
        //+1保证不会发放重复单号
        return beginInteger * i + 1;
    }

    /**
     * 获取单号发放的最大单号
     * @param orderNumberList
     * @return
     */
    public static Long getEndOrderNumber(DOrderNumberList orderNumberList){
        Long startOddId = orderNumberList.getStartOddId();
        Integer grantOrderNumber = orderNumberList.getGrantOrderNumber();
        Long endOrderNumber = startOddId+grantOrderNumber;
        //-1保证不是漏发单号
        return endOrderNumber -1 ;
    }

}
